package com.sys.db.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author wangjb
 * @version $Revision: 1.0 $
 */
public class ExtendDate extends Date {

	/** */
	private static final long serialVersionUID = 1L;

	/**
	 * 得到不带时间的当前日期
	 * @return
	 */
	public static Date getCurrentDateNoTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 得到yyyyMMdd格式当前日期
	 * @return
	 */
	public static String getNowDateShort() {
		java.text.SimpleDateFormat dateFormatShort = new java.text.SimpleDateFormat(
				"yyyyMMdd");
		String ret = dateFormatShort.format(new java.util.Date());
		if (ret == null) {
			ret = "";
		}
		return ret;

	}

	/**
	 * 得到yyyy-MM-dd HH:mm:ss格式当前时间
	 * @return
	 */
	public static String getSysDate() {
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ret = df.format(date);
		return ret;
	}
	/**
	 * 得到yyyy-MM-dd HH:mm格式当前时间
	 * @return
	 */
	public static String getSysN0SecDate() {
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String ret = df.format(date);
		return ret;
	}
	/**
	 * 得到定时的组合日期
	 * @param scrollDate
	 * @return
	 */
	public static Date getTimingAssembledDate(String date, String time) {
		Date d = new Date();
		date = date + " " + time + ":00:01";
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			d = format.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return d;
	}
	/**
	 * 根据设定时间得到后N个月末日期
	 * @param date
	 * @param scrollDate
	 * @return
	 */
	public static String getAheadMonthEndDate(String d,int scrollMonth){
		String[] dc = d.split("-");
		int y, m, da;
		y = Integer.parseInt(dc[0]);
		m = Integer.parseInt(dc[1]);
		da = Integer.parseInt(dc[2]);

		GregorianCalendar currentDate = new GregorianCalendar();

		currentDate.set(y, m - 1, da);
		currentDate.add(Calendar.MONTH, scrollMonth);
		String strDate="";
		int month;
		int day;
		month = currentDate.get(Calendar.MONTH) + 1;
		day=currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
		strDate = "" + currentDate.get(Calendar.YEAR)
		+ (month < 10 ? "0" + month : "" + month)
		+ day;
		return strDate;
	}
	/**
	 * 根据设定时间得到后N个季末日期
	 * @param date
	 * @param scrollDate
	 * @return
	 */
	public static String getAheadSeasonEndDate(String d,int scrollSeason){
		String[] dc = d.split("-");
		int y, m, da;
		y = Integer.parseInt(dc[0]);
		m = Integer.parseInt(dc[1]);
		da = Integer.parseInt(dc[2]);

		GregorianCalendar currentDate = new GregorianCalendar();

		currentDate.set(y, m - 1, da);
		currentDate.add(Calendar.MONTH, (scrollSeason*3));
		String strDate="";
		int month;
		int day;
		month = currentDate.get(Calendar.MONTH) + 1;
		if(month<=3){
			month=3;
		}else if(month<=6){
			month=6;
		}else if(month<=9){
			month=9;
		}else{
			month=12;
		}
		currentDate.set(currentDate.get(Calendar.YEAR), month - 1, da);
		day=currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
		strDate = "" + currentDate.get(Calendar.YEAR)
		+ (month < 10 ? "0" + month : "" + month)
		+ day;
		return strDate;
	}
	/**
	 * date转String
	 * @param 
	 * @return
	 */
	public static String getStringDonversionDate(Date date) {

		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		String str = formatDate.format(date);

		return str;
	}

	/**
	 * 根据当前时间向前加N天
	 * @param scrollDate
	 * @return
	 */
	public static Date getNowDateAheadTime(int scrollDate) {
		Date d = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String strDate = "";
		int date = 0;
		int month = 0;

		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(Calendar.DATE, scrollDate);
		month = currentDate.get(Calendar.MONTH) + 1;
		date = currentDate.get(Calendar.DAY_OF_MONTH);
		strDate = "" + currentDate.get(Calendar.YEAR) + "-"
				+ (month < 10 ? "0" + month : "" + month) + "-"
				+ (date < 10 ? "0" + date : "" + date);

		try {
			d = format.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return d;
	}

	/**
	 * 根据设定时间向前加N天
	 * @param scrollDate
	 * @return
	 */
	public static Date getControlDateAheadTime(String d, int scrollDate) {
		String strDate = "";
		int date = 0;
		int month = 0;
		Date dd = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String[] dc = d.split("-");
		int y, m, da;
		y = Integer.parseInt(dc[0]);
		m = Integer.parseInt(dc[1]);
		da = Integer.parseInt(dc[2]);

		GregorianCalendar currentDate = new GregorianCalendar();

		currentDate.set(y, m - 1, da);
		currentDate.add(Calendar.DATE, scrollDate);
		month = currentDate.get(Calendar.MONTH) + 1;
		date = currentDate.get(Calendar.DAY_OF_MONTH);
		strDate = "" + currentDate.get(Calendar.YEAR) + "-"
				+ (month < 10 ? "0" + month : "" + month) + "-"
				+ (date < 10 ? "0" + date : "" + date);

		try {
			dd = format.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dd;
	}
	/**
	 * 根据设定时间向前加N天 yyyyMMdd
	 * @param scrollDate
	 * @return String
	 */
	public static String getControlDateAheadTimeString(String d, int scrollDate) {
		Date date=getControlDateAheadTime(d,scrollDate);
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		String dd=format.format(date);
		return dd;
	}
	/**
	 * 根据设定时间向前加N个月
	 * @param scrollDate
	 * @return
	 */
	public static Date getDateAheadMonth(String d, int scrollMonth) {
		String strDate = "";
		int date = 0;
		int month = 0;
		Date dd = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String[] dc = d.split("-");
		int y, m, da;
		y = Integer.parseInt(dc[0]);
		m = Integer.parseInt(dc[1]);
		da = Integer.parseInt(dc[2]);

		GregorianCalendar currentDate = new GregorianCalendar();

		currentDate.set(y, m - 1, da);
		currentDate.add(Calendar.MONTH, scrollMonth);
		month = currentDate.get(Calendar.MONTH) + 1;
		date = currentDate.get(Calendar.DAY_OF_MONTH);
		strDate = "" + currentDate.get(Calendar.YEAR) + "-"
				+ (month < 10 ? "0" + month : "" + month) + "-"
				+ (date < 10 ? "0" + date : "" + date);

		try {
			dd = format.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dd;
	}
	/**
	 * 根据设定时间向前加N个月
	 * @param scrollDate
	 * @return String
	 */
	public static String getDateAheadMonthString(String d, int scrollDate) {
		Date date=getDateAheadMonth(d,scrollDate);
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		String dd=format.format(date);
		return dd;
	}
	/**
	 * 比较两个日期是否相等(不带时间值)
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareDateNoTime(Date date1, Date date2) {
		if (date1 == null && date2 == null)
			return 0;
		else if (date1 == null && date2 != null)
			return -1;
		else if (date1 != null && date2 == null)
			return 1;
		else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String date1Str = (String) sdf.format(date1);
			String date2Str = (String) sdf.format(date2);
			return date1Str.compareTo(date2Str);
		}

	}

	/**检查一个日期是否在一个星期内
	 * @param date
	 * @return
	 */
	public static boolean dateWithInWeek(Date date) {
		if (date == null)
			return false;
		else {
			Date currencyDay = new Date(System.currentTimeMillis());
			long subTime = 7 * 24 * 60 * 60 * 1000;
			Date frontDay = new Date(System.currentTimeMillis() - subTime);
			if (date.compareTo(frontDay) >= 0
					&& date.compareTo(currencyDay) <= 0)
				return true;

		}
		return false;

	}

	/**
	 * 检查过去某时间是否在限制的扩展时间内
	 * @param date 过去某时间
	 * @param extendDate 扩展时间
	 * @return
	 */
	public static boolean compareDate(Date date, Date extendDate) {
		if (date == null || extendDate == null) {
			return false;
		} else {
			Date currentDay = new Date(System.currentTimeMillis());
			Date frontDate = new Date(currentDay.getTime()
					- extendDate.getTime());

			if (date.compareTo(frontDate) >= 0
					&& date.compareTo(currentDay) <= 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 检查一个日期是否是在当前时间的前24小时内
	 * @param date
	 * @return
	 */
	public static boolean dateInFrontDay(Date date) {
		if (date != null) {
			Date currencyDay = new Date(System.currentTimeMillis());
			long subTime = 24 * 60 * 60 * 1000;
			Date frontDay = new Date(System.currentTimeMillis() - subTime);
			if (date.compareTo(frontDay) >= 0
					&& date.compareTo(currencyDay) <= 0)
				return true;
		}
		return false;
	}

	/**
	 * 根据合同起始日期计算贷款期限
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static String getLoanTerm(Date startDate, Date endDate) {
		Calendar xCalendar = Calendar.getInstance();
		xCalendar.setTime(startDate);
		Calendar yCalendar = Calendar.getInstance();
		yCalendar.setTime(endDate);

		int year = yCalendar.get(Calendar.YEAR) - xCalendar.get(Calendar.YEAR);
		int month = yCalendar.get(Calendar.MONTH)
				- xCalendar.get(Calendar.MONTH);
		int day = yCalendar.get(Calendar.DAY_OF_MONTH)
				- xCalendar.get(Calendar.DAY_OF_MONTH);

		int totalMonths = year * 12 + month;
		String loanterm = null;
		if (day >= 0)
			totalMonths = year * 12 + month + 1;
		if (totalMonths > 0 && totalMonths < 13) {//短期
			loanterm = "1";
		} else if (totalMonths > 12 && totalMonths < 61) {//中期
			loanterm = "2";
		} else if (totalMonths > 60) {// 长期
			loanterm = "3";
		}
		return loanterm;
	}
	

	/**
	 * 比较两个日期是否同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareSameDay(Date date1, Date date2){
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		
		cal1.setTime(date1);
		cal2.setTime(date2);
		if(cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)){
			return false;
		}
		else if(cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH)){
			return false;
		}
		else{
			if(cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)){
				return true;
			}else
				return false;
		}
		
	}
	/**
	 * 判断两个日期是否同一个礼拜
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareSameWeek(Date date1, Date date2){
		
		  Calendar cal1 = Calendar.getInstance();
		  Calendar cal2 = Calendar.getInstance();
		  cal1.setTime(date1);
		  cal2.setTime(date2);
		  int subYear = cal1.get(Calendar.YEAR)-cal2.get(Calendar.YEAR);
		  //subYear==0,说明是同一年
		  if(subYear == 0)
		  {
		   if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
		    return true;
		  }
		  //例子:cal1是"2005-1-1"，cal2是"2004-12-25"
		  //java对"2004-12-25"处理成第52周
		  // "2004-12-26"它处理成了第1周，和"2005-1-1"相同了
		  //大家可以查一下自己的日历
		  //说明:java的一月用"0"标识，那么12月用"11"
		  else if(subYear==1 && cal2.get(Calendar.MONTH)==11)
		  {
		   if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
		    return true;
		  }
		  //例子:cal1是"2004-12-31"，cal2是"2005-1-1"
		  else if(subYear==-1 && cal1.get(Calendar.MONTH)==11)
		  {
		   if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
		    return true;
		   
		  }
		  return false;
	}
	
	
	/**
	 * 判断两个日期是否同一个月
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareSameMonth(Date date1, Date date2){
		Calendar cal1 = Calendar.getInstance();
		  Calendar cal2 = Calendar.getInstance();
		  cal1.setTime(date1);
		  cal2.setTime(date2);
		  
		  if(cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)){
			  return false;
		  }
		  else {
			  return (cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH))?true:false;
		  }
	}
	
	
	/**
	 * 取得两个日期之间间隔的月数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int getIntervalMonths(Date startDate, Date endDate) {
		Calendar xCalendar = Calendar.getInstance();
		xCalendar.setTime(startDate);
		Calendar yCalendar = Calendar.getInstance();
		yCalendar.setTime(endDate);
		
		int year = yCalendar.get(Calendar.YEAR) - xCalendar.get(Calendar.YEAR);
		int month = yCalendar.get(Calendar.MONTH)
		- xCalendar.get(Calendar.MONTH);
		int day = yCalendar.get(Calendar.DAY_OF_MONTH)
		- xCalendar.get(Calendar.DAY_OF_MONTH);
		
		int totalMonths = year * 12 + month;
		if (day >= 0)
			totalMonths = year * 12 + month + 1;
		return totalMonths;
	}
		
	/**
	 * 将日期格式转化为字符串
	 * @param date
	 * @return 201003161025
	 */
	public static String dateToStringForNDS(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH));
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		return year+month+day+hour+minute;
	}
	/**
	 * 得到两个时间的时间差（分钟）
	 * @param date
	 * @return long
	 */
	public static long getTimeDifferenceMin(String date1,String date2){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		long between=0;
		try {
			Date d1=dfs.parse(date1);
			Date d2=dfs.parse(date2);
			between=Math.abs(d1.getTime()-d2.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long min=between/(60*1000);
		return min;
	}
	/**
	 * 得到两个时间的时间差（秒）
	 * @param date
	 * @return long
	 */
	public static long getTimeDifferenceSecond(String date1,String date2){
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		long between=0;
		try {
			Date d1=dfs.parse(date1);
			Date d2=dfs.parse(date2);
			between=Math.abs(d1.getTime()-d2.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long second=between/1000;
		return second;
	}
}
